package nacos

import (
	"gitee.com/zawei/yggdrasil/pkg"
	"gitee.com/zawei/yggdrasil/pkg/config"
	"gitee.com/zawei/yggdrasil/pkg/log"
	"gitee.com/zawei/yggdrasil/pkg/registry"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
)

type Config struct {
	Client  *constant.ClientConfig
	Servers []constant.ServerConfig
}

func (cfg *Config) Build() *nacosRegistry {
	if cfg.Client == nil {
		cfg.Client = constant.NewClientConfig()
	}
	if cfg.Servers == nil {
		cfg.Servers = []constant.ServerConfig{*constant.NewServerConfig("127.0.0.1", 8848)}
	}
	cfg.Client.AppName = pkg.AppName()
	return newRegistry(cfg)
}

func NewRegistry() registry.Registry {
	cfg := &Config{}
	if err := config.Get("registry.nacos").Scan(cfg); err != nil {
		log.Fatalf("fault to scan config", "name", "nacos", "error", err)
	}
	return cfg.Build()
}
